package com.kedou.net;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.kedou.app.Config;
import com.kedou.app.R;
import com.kedou.domain.Fund;
import com.kedou.util.Util;

/**
 * 后端请求：项目认筹
 * 
 * @author zhouchao
 * 
 */
public class PostGoFund {
	public PostGoFund(String token, String projectId, String userId, String os,
			String deviceId, final SuccessCallback successCallback,
			final FailCallback failCallback, final Context context) {
		new NetConnection(
				Util.getSDBaseUrl() + Config.URL_GOFUND,
				HttpMethod.POST,
				new NetConnection.SuccessCallback() {

					@Override
					public void onSuccess(String result) {
						try {
							JSONObject obj1 = new JSONObject(result);
							JSONObject obj = obj1.getJSONObject("data");
							Fund fund = new Fund();
							switch (obj1.getInt(Config.KEY_STATUSCODE)) {
							case Config.RESULT_STATUS_SUCCESS:
								if (obj.has("fundStatus")) {
									fund.setFundStatus(obj.getInt("fundStatus"));
								}
								if (obj.has("fundDetail")) {
									JSONObject jsonFundDetail = obj
											.getJSONObject("fundDetail");
									if (jsonFundDetail.has("projectName")) {
										fund.setProjectName(jsonFundDetail
												.getString("projectName"));
									}
									if (jsonFundDetail.has("team")) {
										fund.setTeam(jsonFundDetail
												.getString("team"));
									}
									if (jsonFundDetail.has("totalMoney")) {
										fund.setTotalMoney(jsonFundDetail
												.getString("totalMoney"));
									}
									if (jsonFundDetail.has("sellingStock")) {
										fund.setSellingStock(jsonFundDetail
												.getString("sellingStock"));
									}
									if (jsonFundDetail.has("financingStage")) {
										fund.setFinancingStage(jsonFundDetail
												.getString("financingStage"));
									}
									if (jsonFundDetail.has("copies")) {
										fund.setCopies(jsonFundDetail
												.getString("copies"));
									}
									if (jsonFundDetail.has("currentAccounting")) {
										fund.setCurrentAccounting(jsonFundDetail
												.getString("currentAccounting"));
									}
									if (jsonFundDetail.has("eachMoney")) {
										fund.setEachMoney(jsonFundDetail
												.getString("eachMoney"));
									}
									if (jsonFundDetail.has("location")) {
										fund.setLocation(jsonFundDetail
												.getString("location"));
									}
									if (jsonFundDetail.has("fundLeaderDetail")) {
										JSONObject jsonObject = jsonFundDetail
												.getJSONObject("fundLeaderDetail");
										if (jsonObject.has("name")) {
											fund.setName(jsonObject
													.getString("name"));
										}
										if (jsonObject.has("accounting")) {
											fund.setAccounting(jsonObject
													.getString("accounting"));
										}
									}

								}
								if (successCallback != null) {
									successCallback.onSuccess(obj1
											.getString(Config.KEY_STATUSMSG),
											fund);
								}
								break;
							default:
								if (failCallback != null) {
									failCallback.onFail(obj
											.getString(Config.KEY_STATUSMSG));
								}
								break;
							}

						} catch (JSONException e) {
							e.printStackTrace();
							if (failCallback != null) {
								failCallback.onFail(context.getResources()
										.getString(R.string.server_have_wrong));
							}
						}
					}
				}, new NetConnection.FailCallback() {

					@Override
					public void onFail() {
						if (failCallback != null) {
							failCallback.onFail(context.getResources()
									.getString(R.string.server_have_wrong));
						}
					}
				}, Config.KEY_TOKEN, token, Config.KEY_PROJECTID, projectId,
				Config.KEY_USERID, userId, Config.KEY_OS, os,
				Config.KEY_DEVICE, deviceId);
	}

	public static interface SuccessCallback {
		void onSuccess(String msg, Fund fund);
	}

	public static interface FailCallback {
		void onFail(String msg);
	}
}
